<template>
  <div>
    <uni-row>
      <uni-forms ref="feeding" :value="feed" class="form" label-width="200">
        <uni-row :gutter="10">
          <uni-col>
            <uni-title :title="feed.batchNumber" type="h1"></uni-title>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="年份 : " name="yearsMonth">
              <uni-easyinput :value="feed.yearsMonth" disabled></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="日期 : " name="dateForDay">
              <uni-datetime-picker type="date" v-model="feed.dateForDay" disabled
              ></uni-datetime-picker>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="车间 : " name="deptName">
              <uni-easyinput disabled :value="feed.deptName"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="养虫间 : " name="roomName">
				<uni-data-select
				    v-model="feed.roomId"
				    :localdata="thisRoomList"
				></uni-data-select>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="类型 : " name="bugType">
              <uni-easyinput :value="feed.bugType" disabled></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="挑虫日期 : " name="optionMonth">
              <uni-easyinput :value="feed.optionMonth" disabled></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="轮次 : " name="round">
              <uni-easyinput :value="feed.round" disabled></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="周期 : " name="period">
              <uni-easyinput disabled :value="feed.period + '天'"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="下次换料时间" name="nextReloadDay">
              <uni-datetime-picker
                  :clearIcon="false"
                  type="date"
                  v-model="feed.nextReloadDay"></uni-datetime-picker>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="本次换料时间" name="reloadDay">
              <uni-datetime-picker type="date" v-model="feed.reloadDay" disabled
              ></uni-datetime-picker>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="24" :sm="12">
            <uni-forms-item label-align="right" label="饲料批号">
              <uni-easyinput :clearable="false" type="text" v-model="feed.feedBatchNumber"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="灭活数量 : ">
              <uni-easyinput v-model="feed.killNum" type="number"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col>
            <uni-title title="正常饲养换料" type="h1"></uni-title>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="换虫数量 : " >
              <uni-easyinput type="number" v-model="feed.changeNum"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="剩余数量 : " >
              <uni-easyinput type="number" v-model="feed.residue"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="僵虫数量 : ">
              <uni-easyinput type="number" v-model="feed.stiffNum"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="轮僵化率 : " >
              <uni-easyinput v-model="roundStiff" disabled></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="成蛹数量 : " >
              <uni-easyinput type="number" v-model="feed.pupaNum"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="轮蛹化率 : " >
              <uni-easyinput v-model="roundPupa" disabled></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="特殊转虫数量" >
              <uni-easyinput type="number" v-model="feed.specialChange"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="死亡数量 : " >
              <uni-easyinput type="number" v-model="deathNum"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="轮死亡率 : " >
              <uni-easyinput v-model="roundDeath" disabled></uni-easyinput>
            </uni-forms-item>
          </uni-col>


          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="转入车间 : ">
              <uni-data-select
                  v-model="feed.changeDeptId"
                  :localdata="changeDepts"
              ></uni-data-select>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="转入房间 : ">
              <uni-data-select
                  v-model="feed.changeRoomId"
                  :localdata="changeRooms"
              ></uni-data-select>
            </uni-forms-item>
          </uni-col>


          <uni-col>
            <uni-title title="隔离饲养换料" type="h1"></uni-title>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="换虫数量 : " >
              <uni-easyinput type="number" v-model="feed.glChangeNum"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="剩余数量 : " >
              <uni-easyinput type="number" v-model="feed.glResidue"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="僵虫数量 : " >
              <uni-easyinput type="number" v-model="feed.glStiffNum"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="轮僵化率 : " >
              <uni-easyinput v-model="glRoundStiff" disabled></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="成蛹数量 : " >
              <uni-easyinput type="number" v-model="feed.glPupaNum"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="轮蛹化率 : " >
              <uni-easyinput v-model="glRoundPupa" disabled></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="死亡数量 : ">
              <uni-easyinput type="number" v-model="glDeathNum"></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="轮死亡率 : " >
              <uni-easyinput v-model="glRoundDeath" disabled></uni-easyinput>
            </uni-forms-item>
          </uni-col>
          <uni-col :xs="24" :sm="18" v-if="showGlNum">
            <uni-forms-item label-align="right" label="隔离批号预览 : ">
              <uni-easyinput v-model="feed.remark"></uni-easyinput>
            </uni-forms-item>
          </uni-col>

          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="隔离转入车间 : ">
              <uni-data-select
                  v-model="feed.glChangeDeptId"
                  :localdata="changeDepts"
              ></uni-data-select>
            </uni-forms-item>
          </uni-col>


          <uni-col :xs="12" :sm="8">
            <uni-forms-item label-align="right" label="隔离转入房间 : ">
              <uni-data-select
                  v-model="feed.glChangeRoomId"
                  :localdata="glChangeRooms"
              ></uni-data-select>
            </uni-forms-item>
          </uni-col>


        </uni-row>
        <button v-if="type == '0'" form-type="submit" class="button" @click="handleSubmit">确认修改</button>
      </uni-forms>
    </uni-row>
  </div>
</template>

<script setup lang="ts">

import UniRow from "../../uni_modules/uni-row/components/uni-row/uni-row.vue";
import UniTitle from "../../uni_modules/uni-title/components/uni-title/uni-title.vue";
import {ref, toRef, toRefs, watchEffect} from "vue";
import {FeedFormHandle} from "../../pagesVM/feed";
import UniForms from "../../uni_modules/uni-forms/components/uni-forms/uni-forms.vue";
import UniCol from "../../uni_modules/uni-row/components/uni-col/uni-col.vue";
import UniFormsItem from "../../uni_modules/uni-forms/components/uni-forms-item/uni-forms-item.vue";
import UniEasyinput from "../../uni_modules/uni-easyinput/components/uni-easyinput/uni-easyinput.vue";

import {onReady} from "@dcloudio/uni-app";
import {FeedMapper} from "../../mappers/FeedMapper";
import {LocalChange} from "../../sql/domin";
import {LocalChangeMapper} from "../../mappers/LocalChangeMapper";
import {ChangeStatus, ChangeType, TargetType} from "../../util/Constants";
import {useStore} from "vuex";
import {parseTime} from "../../util/auth";

const store = useStore()

const {
  feed,
    showGlNum,
  rules,
  glRoundDeath,
  glDeathNum,
  glRoundPupa,
  glRoundStiff,
  roundDeath,
  deathNum,
  roundPupa,
  roundStiff,
  changeRooms,
  changeDepts,
  glChangeRooms,
  thisRoomList,
  type
} = FeedFormHandle()
const feeding = ref()
onReady(() => {
  feeding.value.setRules(rules)
})

const handleSubmit = () => {
  let feedInfo = feed.value
  //(feedInfo)
	delete feedInfo.deptName
	delete feedInfo.roomName
	feedInfo.updatedTime = parseTime(Date()) 
    FeedMapper.updateFeedById(feedInfo, 'Feed').then(() => {
      LocalChangeMapper.insertLocalChange({
        targetId: feedInfo.id,
        targetType: TargetType.FEED,
        changeType: ChangeType.upd,
        userId: store.getters.userId,
        changeTime: parseTime(new Date()),
        changeStatus: ChangeStatus.UNSYNC
      } as LocalChange).then(() => {
        uni.navigateBack()
      })
    })

}

</script>

<style scoped>
.form {
  width: 80%;
  font-size: 50px;
  margin: 30px auto 0 auto
}

.button {
  width: 200px;
  background-color: #fc4f42;
  border-radius: 30px;
  color: white;
  margin-bottom: 20px;
}
</style>
